package br.trainee.web.dao.impl;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

import br.trainee.web.dao.service.IStatusDAO;
import br.trainee.web.vo.StatusVO;

public class StatusDAOImpl extends EntityDAOImpl<StatusVO, Long> implements IStatusDAO{
	
	@PersistenceContext(unitName="WebJsf")
	private EntityManager em;
	
	@Resource
	private UserTransaction userTransaction;
	
	
	public StatusDAOImpl() {
		
		super(StatusVO.class);
	}

	@Override
	public EntityManager getEntityManager() {
		
		return this.em;
	}

	@Override
	public UserTransaction getUserTransaction() {
		
		return this.userTransaction;
	}

	@Override
	public StatusVO consultarStatusPorTipo(Integer tipoStatus) throws Exception{
		
		StatusVO status = null;
		
		try{
			
			Query query = em.createNamedQuery("buscarStatusPorTipo");
			query.setParameter("tipoStatus", tipoStatus);
			//query.setHint("org.hibernate.cacheable", true);
			
			status = (StatusVO) query.getSingleResult();
			
		}
		catch(NoResultException e){
			
			status = null;
		}
		
		return status;
	}
	

}
